<template>
    <view>
        <u-upload
            :fileList="showList"
            @afterRead="afterRead"
            @delete="deletePic"
            multiple
            :maxCount="10"
			:sizeType="['compressed']"
            :previewFullImage="true"
            accept="image"
            :capture="['album', 'camera']"
            width="80"
            height="80"
        ></u-upload>
    </view>
</template>

<script>
// 使用方法 <base-upload v-model="model.images"/>
import cache from '@/config/cache.js'
export default {
	name:"base-upload",
	model: {
	  prop: 'value', // 默认是value
	  event: 'input', // 默认是input
	},
    props: {
        value: {
            type: Array,
            default: [],
            required: true
        },
    },
    data() {
        return {
            files: [],
            // fileList: [{url:'uploads/wechat/202301/63bd1e1f9c402.png'}],
            fileList: [],
			token:'',
        };
    },
  created() {
    this.token = cache.get("token")
  },
    computed: {
        showList () {
            return this.fileList.map(item => {
                if (item.url && !item.url.startsWith('http')) {
                   item.url = this.$settings.imgBaseURL + item.url;
                }
				
                return item
            })
        }
    },
	watch: {
		
		value: {
			immediate: true,
			handler(newVal, oldVal) {
				if (newVal) {
					this.files = newVal
					this.fileList = newVal
				} else {
					this.files = [];
					this.fileList = [];
				}
			}
		}
	},

    methods: {
        async afterRead(event) {
            try {
                let lists = [].concat(event.file)
				this.fileList = this.fileList.concat(lists)
				
               //  let fileListLen = this.fileList.length;
               //  lists.map((item, index) => {
               //      const itemKey = index + fileListLen;
               //      this.fileList.push({
               //          ...item,
               //          status: 'uploading',
               //          message: '上传中'
               //      })
               //  })
               
               //  for (let i = 0; i < lists.length; i++) {
               //      const result = await this.uploadFilePromise(lists[i].url);
               //      const itemKey = i + fileListLen;
               //      const time = new Date().getTime();
               //      this.fileList[itemKey] = {
               //          name: time,
               //          url: JSON.parse(result.data).data
               //      };
               //  }

               
				this.$nextTick(() => {
				    this.$emit("input", this.fileList);
					this.$emit('change', this.fileList);
				});
            } catch (e) {
            }
        },
        uploadFilePromise(url) {
            let uploadUrl = this.$settings.baseURL+'/wechat/work.Upload/upload';

            return new Promise(resolve => {
                uni.uploadFile({
                    url: uploadUrl, // 仅为示例，非真实的接口地址
                    filePath: url,
                    name: 'file',
                    header: {
						'AuthToken':this.token, //从缓存中获取
						'content-type': 'multipart/form-data'
                    },
					formData: {
						parkId: '1'
					},
                    success: res => {
                        setTimeout(() => {
                            resolve(res);
                        }, 1000);
                    },
                    fail: res => {
                        setTimeout(() => {
                            resolve(res);
                        }, 1000);
                    }
                });
            });
        },
        // 删除图片
        deletePic(event) {
            const itemKey = Object.keys(this.files)[event.index];
            delete this.files[itemKey];
            this.fileList = Object.values(this.files);
            this.$emit('change', this.fileList);
			this.$nextTick(() => {
			    this.$emit("input", this.fileList);
			});
        }
    }
};
</script>

<style>
	
</style>
